
module DrunkenMaster

  class FileHandler
    def initialize root
      unless File.directory?(root)
        raise "No such directory #{root}"
      end
      @root = root
      @handler = Rack::File.new(root)
    end

    def execute(context)
      if context.tail
        context.rack_env['PATH_INFO'] = context.tail.join('/')
      end
      r = context.response
      r.code, r.headers, r.body =
        @handler.call(context.rack_env)
    end
    
    def match_tail? tail      
      (tail =~ /\.[A-Za-z]+$/) && File.exist?(@root+'/'+tail)
    end
  end
    
end